﻿// -----------------------------------------------------------------------
//  <copyright file="SettingsControllerBase.cs" company="LiuliuSoft">
//      Copyright (c) 2025 66SOFT. All rights reserved.
//  </copyright>
//  <site>https://ifs.66soft.net</site>
//  <last-editor>郭明锋</last-editor>
//  <last-date>2025-10-06 00:10</last-date>
// -----------------------------------------------------------------------

using DaprPlus.Domain;
using DaprPlus.Settings.Application.Commands;
using DaprPlus.Settings.Application.Queries;
using DaprPlus.Settings.Domain.Dtos;


namespace DaprPlus.Settings.Controllers;

public abstract class SettingsControllerBase(IMediator mediator) : ApiControllerBase
{
    public abstract Dictionary<string, string> SettingTypeFullNameDict { get; }

    [HttpGet("item")]
    [Produces<SettingItemOutDto>]
    public async Task<IActionResult> GetItem(string typeName, string key)
    {
        if (!SettingTypeFullNameDict.TryGetValue(typeName, out var typeFullName))
        {
            return Ok(ApiResult.Error($"未找到设置项类型：{typeName}"));
        }

        var query = new GetSettingItemByTypeNameAndKeyQuery(typeFullName, key);
        var result = await mediator.Send(query);
        return Ok(result);
    }

    [HttpGet]
    [Produces<SettingDefinitionOutDto>]
    public async Task<IActionResult> Get(string typeName)
    {
        if (!SettingTypeFullNameDict.TryGetValue(typeName, out var typeFullName))
        {
            return Ok(ApiResult.Error($"未找到设置项类型：{typeName}"));
        }

        var query = new GetSettingByTypeNameQuery(typeFullName);
        var result = await mediator.Send(query);
        return Ok(result);
    }

    [HttpPost("reset")]
    [Produces<ApiResult<SettingDefinitionOutDto>>]
    public async Task<IActionResult> Reset(string typeName)
    {
        if (!SettingTypeFullNameDict.TryGetValue(typeName, out var typeFullName))
        {
            return Ok(ApiResult.Error($"未找到设置项类型：{typeName}"));
        }

        var command = new ResetSettingCommand(typeFullName);
        var result = await mediator.Send(command);
        return Ok(result);
    }

    [HttpPut]
    [Produces<ApiResult>]
    public async Task<IActionResult> Update([FromBody] SettingDefinitionInDto dto)
    {
        var command = new UpdateSettingCommand(dto);
        var result = await mediator.Send(command);
        return Ok(result);
    }
}